package com.xiguaji.tiktok.v1.tiktok.permission;


import com.xiguaji.tiktok.utils.AspectUtil;
import com.xiguaji.tiktok.utils.Ret;
import com.xiguaji.tiktok.v1.tiktok.permission.validate.Validate;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 路由访问查看
 * @author zzy
 * @Date 2019-07-18 15:25
 */
@Slf4j
@Aspect
@Component
public class RequestLimitsAspect extends ValidateAspect{

    @Pointcut(value = "@annotation(com.xiguaji.tiktok.v1.tiktok.permission.RequestLimits)")
    public void Pointcut(){
    }

    @Around(value = "Pointcut()")
    public Object printInfo(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        val requestLimits = AspectUtil.INSTANCE.getMethod(proceedingJoinPoint).getAnnotation(RequestLimits.class);
        val requestLimitList = requestLimits.value();

        for (RequestLimit requestLimit : requestLimitList) {
            Validate validate = validateFactory.getValidate(requestLimit);
            boolean allow = requestLimitValidate(requestLimit,proceedingJoinPoint,validate);

            if (!allow){
                return Ret.noPermission(validate.tips);
            }
        }
        return proceedingJoinPoint.proceed();
    }
}
